import os, shutil, zipfile
import releaseconf

APPNAME = "calculator"

PATH = releaseconf.PATH # example: "d:/code/firefoxcalculator/"
DEST = releaseconf.DEST # example: "d:/cave/release/firefoxcalculatorr/"
CALCVERSION = ["1.1.26", "1.1.25", "1.1.24", "1.1.23", "1.1.22", "1.1.21", "1.1.20", "1.1.19", "1.1.18", "1.1.17", "1.1.16", "1.1.15", "1.1.14", '1.1.13', '1.1.12']
FIREFOXMAX = ["10.*", "8.*", "7.*", "4.0.*", "4.0b8pre", "3.7a1pre", '3.5.*', '3.2a1pre', '3.1']
DATE = ["2012.2.14", "2012.2.8", "2012.2.6", "2011.9.25", "2011.7.31", "2011.3.20", "2010.10.27", "2010.07.04", "2010.06.16", "2010.01.18", "2009.11.13", "2009.09.19", '2009.07.25', '2008.12.28', '2008.09.27']

def updatefiles(files):
    for f in files:
        # read
        fs = open(f, "r")
        all = fs.read()
        fs.close()

        # replace
        all = all.replace(CALCVERSION[1], CALCVERSION[0]);
        all = all.replace(FIREFOXMAX[1], FIREFOXMAX[0]);
        all = all.replace(DATE[1], DATE[0]);
        #write
        fs = open(f, "w")
        fs.write(all)
        fs.close()
    print "updated!"
if 1:
    updatefiles([PATH+"install.js",
                 PATH+"install.rdf",
                 PATH+"chrome/content/preferences.xul",
                 PATH+"chrome/content/help.xhtml",
                 PATH+"chrome/content/about.xhtml",
                 ])

def release():
    # get the extension code
    try:
        shutil.rmtree(DEST)
    except:
        pass
    os.system("hg archive \"%s\"" % (DEST))
    for x in ["release.py", "redirect.html", "Screenshot.jpg"]:
        os.remove(DEST+x)

    # set the dir structure
    os.rename(DEST+"chrome", DEST+"chromefull")
    os.makedirs(DEST+"chrome")
    command = "cd %s && zip %s.jar -r *" % (DEST+"chromefull", APPNAME)
    os.system(command)
    shutil.move("%schromefull/%s.jar" % (DEST, APPNAME),
                "%s/chrome" % DEST)
    shutil.rmtree(DEST+"chromefull")
    os.system("cd %s && zip %s.xpi -r *" % (DEST, APPNAME))
    shutil.move("%s%s.xpi" % (DEST, APPNAME), "%s%s.xpi" % (PATH, APPNAME))
    try:
        shutil.rmtree(DEST)
        shutil.rmtree("%s%s.xpi" % (DEST, APPNAME))
    except:
        pass


release()
